<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.io.File" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.io.IOException" %>
<%@ page import="org.springframework.util.FileCopyUtils" %>
<%@ page import="com.lgcns.commons.WebContents" %>
<%
	response.setContentType("application/octet-stream");

	request.setCharacterEncoding("UTF-8");
	String filename = request.getParameter("filename");

	File file = new File(WebContents.BASE_PATH + filename);
	
	response.setContentLength((int) file.length());

	String userAgent = request.getHeader("User-Agent");
	boolean ie = userAgent.indexOf("MSIE") > -1;
	
	String fileName = null;

	if (ie) {
		fileName = URLEncoder.encode(file.getName(), "UTF-8");
	} else {
		fileName = new String(file.getName().getBytes("UTF-8"), "ISO-8859-1");
	}
	
	response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
	response.setHeader("Content-Transfer-Encoding", "binary");
	
	out.clear();
	
	OutputStream outputStream = response.getOutputStream();
	FileInputStream fis = null;

	try {
		fis = new FileInputStream(file);
		FileCopyUtils.copy(fis, outputStream);
	} finally {
		if (fis != null) {
			try {
				fis.close();
			} catch (IOException e) {}
		}
	}
	
	out.flush();
%>